package com.grudesign.stutrip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.grudesign.stutrip.common.ReturnInfo;
import com.grudesign.stutrip.common.TripException;
import com.grudesign.stutrip.entity.Vo.userInfoVo;
import com.grudesign.stutrip.entity.Po.userInfoPo;
import com.grudesign.stutrip.entity.Vo.userQueryVo;
import com.grudesign.stutrip.mapper.userInfoMapper;
import com.grudesign.stutrip.service.userInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grudesign.stutrip.utils.RandomCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 普通用户表 服务实现类
 * </p>
 *
 * @author wx
 * @since 2022-04-14
 */
@Service
@Slf4j
public class userInfoServiceImpl extends ServiceImpl<userInfoMapper, userInfoPo> implements userInfoService {
//    默认用户头像
    public final static String DEFAULT_HEAD ="/shuai-file.oss-cn-beijing.aliyuncs.com/2021/09/25/fe948166file.png";
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public Object register(userInfoVo reqVo) {
        log.info("入参的注册信息{}",reqVo);
        String checkCode = reqVo.getCheckCode();
        if(!Objects.equals(redisTemplate.opsForValue().get(reqVo.getMobile()), checkCode)){
            throw  new TripException(-1,"验证码错误");
        }
        userInfoPo userInfoPo = new userInfoPo();
        BeanUtils.copyProperties(reqVo,userInfoPo);
        Integer integer = baseMapper.selectCount(new QueryWrapper<userInfoPo>().eq("mobile", reqVo.getMobile()));
        if(integer>0){
            throw new TripException(-1,"用户已经被注册");
        }
        userInfoPo.setHeadPortrait(DEFAULT_HEAD);
        int insert = baseMapper.insert(userInfoPo);
        return insert;
    }

    @Override
    public Object sendCheckCode(String phone) {
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return code;
        }
        code = RandomCode.SixCode();
        redisTemplate.opsForValue().set(phone,code,1, TimeUnit.MINUTES);
        log.info("手机号为:{},验证码为:{}",phone,code);
        return code;
    }

    @Override
    public Object login(userInfoVo reqVo) {
        String mobile =reqVo.getMobile();
        String password = reqVo.getPassword();
        String checkCode = reqVo.getCheckCode();
        QueryWrapper <userInfoPo> queryWrapper = Wrappers.query();
        queryWrapper.eq("mobile",mobile);
        userInfoPo userInfoPo = baseMapper.selectOne(queryWrapper);
        if(StringUtils.hasText(password)){
            if(null==userInfoPo){
                return new ReturnInfo("-1","err").data("errMsg","用户不存在");
            }
            if(!password.equals(userInfoPo.getPassword())){
                return new ReturnInfo("-1","err").data("errMsg","用户密码错误");
            }
            if(userInfoPo.getIsDisabled()){
                return new ReturnInfo("-1","err").data("errMsg","用户被禁用");
            }

            return new ReturnInfo("200","ok").data("userInfo",userInfoPo);
        }
        if(StringUtils.hasText(checkCode)){
            this.loginByCode(reqVo);
            return new ReturnInfo("200","ok").data("userInfo",userInfoPo);
        }
        return new ReturnInfo("-1","error");
    }

    @Override
    public Object getQueryUsers(long cur,long next,userQueryVo queryVo) {
        String nickname = queryVo.getNickname();
        String phone = queryVo.getMobile();
        Page<userInfoPo> page = new Page<>(cur,next);
        QueryWrapper<userInfoPo> userConditionWrapper = Wrappers.query();
        if(StringUtils.hasText(nickname)){
            userConditionWrapper.like("nickname",nickname);
        }
        if(StringUtils.hasText(phone)){
            userConditionWrapper.like("mobile",phone);
        }
        Page<userInfoPo> userInfoPoPage = baseMapper.selectPage(page, userConditionWrapper);
        return new ReturnInfo("200","ok").data("users",userInfoPoPage);
    }

    public void loginByCode(userInfoVo reqVo) {
        String phone = reqVo.getMobile();
        String checkCode =reqVo.getCheckCode();
        String redisCode = redisTemplate.opsForValue().get(phone);
        if(!redisCode.equals(checkCode)){
            throw new TripException(-1,"验证码错误");
        }
    }
}
